﻿using System.Drawing;

namespace GraphDrawing.Graph.Model
{
    /// <summary>
    /// Модель связи между двумя слотами.
    /// </summary>
    public class ConnectionModel
    {
        /// <summary>
        /// Слот-источник.
        /// </summary>
        public SlotModel From { get; private set; }

        /// <summary>
        /// Слот-приёмник.
        /// </summary>
        public SlotModel To { get; private set; }

        /// <summary>
        /// Тип соединителя.
        /// </summary>
        public LineShapeType ShapeType { get; private set; }

        /// <summary>
        /// Флаг, равный true, если соединительная линия выделена, иначе false.
        /// </summary>
        public bool IsSelected { get; set; }

        public ConnectionModel(LineShapeType shapeType, SlotModel @from, SlotModel to)
        {
            ShapeType = shapeType;
            From = @from;
            To = to;
            IsSelected = false;
        }

        /// <summary>
        /// Проверяет, принадлежит ли точка части линии.
        /// </summary>
        /// <param name="point">Координаты точки в глобальных координатах.</param>
        public bool HitTest(Point point)
        {
            var r1 = From.GetBounds();
            var r2 = To.GetBounds();
            var p1 = new Point(r1.X + r1.Width/2, r1.Y + r1.Height/2);
            var p2 = new Point(r2.X + r2.Width/2, r2.Y + r2.Height/2);
            using (var path = GraphicsPathFactory.CreatePath(ShapeType, p1, p2))
            using (var wideningPen = new Pen(Color.Black, 5))
            {
                path.Widen(wideningPen);
                return path.IsOutlineVisible(point, wideningPen);
            }
        }
    }
}